package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.GoodsDetail;
import com.qf.mapper.GoodsDetailMapper;
import com.qf.service.GoodsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 10537
 * @version V1.0
 * @Date 2021/10/5 17:57
 */
@Service
public class GoodsDetailServiceImpl implements GoodsDetailService {
    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Override
    public void modifyById(GoodsDetail goodsDetail) {
        GoodsDetail gd = goodsDetailMapper.selectByPrimaryKey(goodsDetail.getId());
        gd.setRemaining(gd.getRemaining()-goodsDetail.getRemaining());
        int i = goodsDetailMapper.updateByPrimaryKey(gd);
        if (i!=1){
            System.out.println("够买产品失败");
            throw new RuntimeException("购买产品失败");
        }
    }

    @Override
    public PageInfo<GoodsDetail> findBySortAndPage(Integer page, Integer rows, Integer sortId) {
        PageHelper.startPage(page,rows);
        List<GoodsDetail> list = goodsDetailMapper.selectBySort(sortId);
        PageInfo<GoodsDetail> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
}
